package com.admgt.resource.dao;

import java.util.Calendar;
import java.util.List;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.admgt.resource.entity.LoadingInfo;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/app-config.xml"})
public class LoadingDaoTest {
	private LoadingInfoDao dao;
	@Autowired
	public void setDao(LoadingInfoDao dao) {
		this.dao = dao;
	}
	@Test
	public void testAddLoadingInfo(){
		LoadingInfo entity = new LoadingInfo();
		entity.setFileName("my-love.mp3");
		entity.setFileSize("5M");
		entity.setHostName("10.225.106.40");
		entity.setLoadProgress(80);
		entity.setLoadStatus("unfinished");
		entity.setLoadType("up");
		entity.setLocalPath("C:/music/westlife");
		entity.setServerPath("/music");
		entity.setUpdatedTime(Calendar.getInstance().getTime());
		System.out.println(entity.toString());
		int n = dao.addLoadingInfo(entity);
		System.out.println("n="+n);
		int id  = dao.getLoadingInfo(n).getId();
		Assert.assertEquals(n,id);
	}
	@Test
	public void testRetriveData(){
		List<LoadingInfo> list = dao.findAllLoadingInfos();
		int size =  list!=null?list.size():0;
		System.out.println(list.get(0).toString());
		Assert.assertEquals(1,size);
	}
	@Test 
	public void testUpdateData(){
		List<LoadingInfo> list = dao.findAllLoadingInfos();
		LoadingInfo entity = list.get(0);
		entity.setLoadProgress(90);
		dao.addLoadingInfo(entity);
		
		LoadingInfo info = dao.getLoadingInfo(entity.getId());
		
		System.out.println(list.get(0).toString());
		Assert.assertEquals(90,info.getLoadProgress());
	}
	@Test
	public void testDeleteData(){
		LoadingInfo entity = dao.getLoadingInfo(1);
		
		dao.removeLoadingInfo(entity);
		
		LoadingInfo info = dao.getLoadingInfo(1);
		
		Assert.assertNull(info);
	}
}
